package com.yl.immodule.func;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;
import com.google.android.material.textfield.TextInputEditText;
import com.android.yl.ylicpsdk.ICPRtcEventHandler;
import com.android.yl.ylicpsdk.ICPRtcManager;
import com.unionbroad.greendao.Channel;
import com.unionbroad.greendao.User;
import com.yl.immodule.R;
import com.yl.immodule.widget.MaterialSelectDialog;

import java.util.List;

public class AddChannelFunc {

    private final Context context;
    private List<User> users;
    private String channelName;

    public AddChannelFunc(Context context) {
        this.context = context;
    }

    public void start() {
        MaterialSelectDialog dialog = new MaterialSelectDialog(context);
        dialog.setUsers(ICPRtcManager.getsInstance().getContactsUserList());
        dialog.setOnDismissListener(dialogInterface -> {
            users = dialog.getSelectedUsers();
            if (users != null && !users.isEmpty()) {
                showInputNameDialog();
            }
        });
        dialog.show();
    }

    private void showInputNameDialog() {
        if (users == null || users.isEmpty()) {
            return;
        }

        Context dialogContext = new android.view.ContextThemeWrapper(
            context, 
            R.style.MaterialDialogStyle
        );
        
        View dialogView = LayoutInflater.from(dialogContext).inflate(
            R.layout.dialog_input_channel_name, null
        );
        
        TextInputLayout inputLayout = dialogView.findViewById(R.id.input_layout);
        TextInputEditText inputEdit = dialogView.findViewById(R.id.input_edit);
        
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(dialogContext)
            .setTitle("输入群组名称")
            .setView(dialogView)
            .setPositiveButton("确定", null)
            .setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        
        AlertDialog dialog = builder.create();
        dialog.setOnShowListener(dialogInterface -> {
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(view -> {
                String name = inputEdit.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    inputLayout.setError("请输入群组名称");
                    return;
                }
                if (name.length() > 13) {
                    inputLayout.setError("群组名称不能超过13个字符");
                    return;
                }
                
                channelName = name;
                dialog.dismiss();
                create();
            });
        });
        
        dialog.show();
    }

    private void create() {
        ICPRtcManager.getsInstance().createAudioPTTChannel(channelName, users, new ICPRtcEventHandler.Callback<Channel>() {
            @Override
            public void onResponse(final Channel channel) {
                new Handler(context.getMainLooper()).post(() -> 
                    Toast.makeText(context, channel != null ? "创建成功" : "创建失败", Toast.LENGTH_SHORT).show()
                );
            }
        });
    }
}
